package com.leetcode.链表;

import java.util.HashMap;

//随机链表的复制
public class lc_138 {
    public static void main(String[] args) {

    }

    public Node copyRandomList(Node head){
        //思路: 使用hashmap存储原链表和复制链表的关系
        HashMap<Node,Node> map = new HashMap<>();
        Node cur = head;
        //遍历新建新的结点
        while(cur != null){
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        //遍历获取原链表的关系
        cur = head;
        while(cur != null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}